﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the Microsoft Public License (Ms-PL).
// See accompanying file License.txt or copy at http://mews.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Mews.Mime
{
    using System;
    using System.Collections.Generic;
    using System.IO;

    /// <summary>Represents a header field that contains references (message-IDs) to one or more other messages (see
    /// <a href="http://www.ietf.org/rfc/rfc5322.txt">RFC 5322</a> 3.6.4.).</summary>
    /// <threadsafety static="true" instance="false"/>
    public sealed class ReferencesHeaderField : HeaderField<ReferencesHeaderField>
    {
        private readonly List<string> references = new List<string>(10);

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>Initializes a new instance of the <see cref="ReferencesHeaderField"/> class.</summary>
        /// <param name="name">The name of the header field, usually <see cref="HeaderFieldNames.References"/>.</param>
        /// <exception cref="ArgumentException"><paramref name="name"/> is <c>null</c> or empty.</exception>
        public ReferencesHeaderField(string name) : base(name)
        {
        }

        /// <summary>Gets a collection containing all references.</summary>
        /// <remarks>Parsed elements in the collection are never <c>null</c>.</remarks>
        public IList<string> References
        {
            get { return this.references; }
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        internal sealed override void WriteTo(Stream stream)
        {
            HeaderFieldHelper.FoldHeaderField(
                this.Name, new[] { new HeaderFieldBodyPart(string.Join(" ", this.references.ToArray()), false) }, stream);
        }
    }
}
